/**
 * Created on 24.07.2012
 */
package com.example.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.ejb.exceptions.DatabaseException;
import com.example.ejb.exceptions.UserAlreadyExistsException;
import com.example.ejb.model.DbUser;
import com.example.utils.UsersDelegate;

/**
 * @author Ciprian Mihalache
 *
 */
public class UserOperationsServlet extends HttpServlet {
	private static final long serialVersionUID = -4543019566300293573L;

	public static final String JSP_NEW_OK = "/WEB-INF/res/new_ok.jsp";
	public static final String JSP_NEW_NOK = "/WEB-INF/res/new_nok.jsp";

	private UsersDelegate delegate;

	public static class METHOD {
		public static String USERS_DISPLAY = "display";
		public static String USERS_EDIT = "/edit";
		public static String USERS_DELETE = "/delete";
		public static String USERS_NEW = "/new";
	}

	public static class ATTRIBUTE {
		public static String DURATION = "DURATION";
		public static String ADD_EXCEPTION = "ADD_EXCEPTION";
	}

	@Override
	public void init() throws ServletException {
		delegate = new UsersDelegate();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getPathInfo();
		if (method == null) {
			resp.sendRedirect(METHOD.USERS_DISPLAY);
		} else if (method.equals(METHOD.USERS_NEW)) {
			addUser(req, resp);
		} else if (method.equals(METHOD.USERS_EDIT)) {
			editUser(req);
		} else {
			resp.sendRedirect(METHOD.USERS_DISPLAY);
		}
	}

	private void addUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String jspPath = JSP_NEW_OK;
		long start = System.currentTimeMillis();

		try {
			addUserToDb(req);
		} catch (UserAlreadyExistsException e) {
			// TODO log exception and perform the appropriate action
			// However, this should not happen, since this class builds the User
			e.printStackTrace();
			req.setAttribute(ATTRIBUTE.ADD_EXCEPTION, e);// ?
			jspPath = JSP_NEW_NOK;// ?
		} catch (DatabaseException e) {
			e.printStackTrace(); // TODO log exception
			req.setAttribute(ATTRIBUTE.ADD_EXCEPTION, e);
			jspPath = JSP_NEW_NOK;
		}

		req.setAttribute(ATTRIBUTE.DURATION, System.currentTimeMillis() - start);
		req.getRequestDispatcher(jspPath).forward(req, resp);
	}

	private void addUserToDb(HttpServletRequest req) throws UserAlreadyExistsException,
			DatabaseException {
		DbUser user = new DbUser();
		user.setName(req.getParameter("name"));
		user.setAddress(req.getParameter("address"));
		user.setMail(req.getParameter("email"));
		user.setPhone(req.getParameter("phone"));
		user.setId(req.getParameter("id"));

		delegate.addUser(user);
	}

	private void editUser(HttpServletRequest req) {
		// TODO Auto-generated method stub
	}

}
